﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Framework.Graphics;
using Framework.Map;
using Framework.Graphics.Light;
using Framework.GameObjectManagment;
using Framework.GameObjectManagment.GameObjects;
using Framework.Physics.Collision;

namespace Framework.GameStateManagment.GameScreens
{
    class SinglePlayer : GameScreen
    {
        TileMap Map;
        GameObjectManager _gbm;
        Texture2D _tile;

        public static bool Move;

        public override void Init()
        {
            Map = new TileMap();
            Map.TileWidth = 100;
            Map.TileHeight = 100;
            Move = true;


            _gbm = new GameObjectManager();
            _gbm.Init();

            Enemy en1 = _gbm.GameObjects[1] as Enemy;
            en1.Target = new Vector2(512, 362);


            //LM = new LightManager();

            //Map = new TileMap.TileMap();
            //Map.Init();
            //// LM.Init(); --> Wird von Game1.cs aufgerufen
            //GameObjektManager.Init();

            base.Init();
        }

        public override void LoadContent(ContentManager Content)
        {
            Map.Initalisieren(Content, "Test");
            _gbm.LoadContent(Content);
            _tile = Content.Load<Texture2D>("Black");
            //Map.LoadContent(Content);
            ////Spieler.LoadContent(Content);
            ////LM.LoadContent(Content);
            //GameObjektManager.LoadContent(Content);

            base.LoadContent(Content);
        }

        public override void Update()
        {
            //Map.Update();
            //LM.Update();
            //GameObjektManager.Update();
            //Camera.Update(Input.Movement);
            Player pl = _gbm.GameObjects[0] as Player;


            bool collision = false;
            for (int y = 0; y < Map.MapHeight; y++)
            {
                for (int x = 0; x < Map.MapWidth; x++)
                {
                    if (Map.AllTiles[x, y].TileEnterable == false)
                    {
                        if (CollisionEasy.Check(new Vector2(x * 50, y * 50)+Input.Movement, _tile, new Vector2(512, 362), pl.Back) == true)
                        {
                            collision = true;
                        }
                    }
                }
            }

            if (collision == false)
            {
                Move = true;
                Input.Movement += Input.MovementNow();
                Camera.CustomUpdate(Input.Movement);
            }
            else
            {
                Move = false;
                Input.Movement += Input.MovementNow() * -1f;                
            }



            _gbm.Update();
            base.Update();
        }

        public override void Draw()
        {
            Camera.SpriteBatch.Begin();
            Map.Draw();
            Camera.SpriteBatch.End();



            LightManager.Draw(Map, _gbm);
            _gbm.Draw();
            //Map.Draw();ja
            //LM.Draw(Map);
            //GameObjektManager.Draw();

            base.Draw();
        }
    }
}
